# coding=utf-8

class Robot:
    '''表示一个带有名字的机器人。'''

    # 一个类变量，表示机器人的数量
    population = 0

    def __init__(self,name):
        '''初始化数据'''
        # 初始化，相当于java中的构造函数
        self.name = name
        print("(Initializing {})".format(self.name))
        #创建机器人时，数量+1
        Robot.population += 1

    def die(self):
        '''我挂了'''
        print("{} is being destroyed".format(self.name))
        #销毁机器人时，数量-1
        Robot.population -= 1

        if Robot.population == 0:
            print("{} was the last one".format(self.name))
        else:
            print("There are still {:d} robots working".format(Robot.population))

    def say_hi(self):
        '''来自机器人的问候'''
        print("Greetings,my masters call me {}".format(self.name))

    @classmethod
    def how_many(cls):
        '''打印当前机器人的数量'''
        print("We have {:d} robots".format(Robot.population))
# class is over

droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()

droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()

print("\nRobots can do some work here.\n")

print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()
